#include<stdio.h>
#include<pthread.h>
void *write1(void*arg)
{
	int count=15;
	while(count-->0)
	{
		printf("write thread1\n");
		sleep(1);
	}
	return (void*)1;
}
void *write2(void*arg)
{
	int count=18;
	while(count-->0)
	{
		printf("write thread2\n");
		sleep(1);
	}
	pthread_exit((void*)2);
	
}
void *write3(void*arg)
{
	int count=20;
	while(count-->0)
	{
		printf("write thread3\n");
		sleep(1);
	}
}

int main()
{
	pthread_t thread1=0;
	pthread_t thread2=0;
	pthread_t thread3=0;
	int ret=pthread_create(&thread1,NULL,write1,NULL);
	ret=pthread_create(&thread2,NULL,write2,NULL);
	ret=pthread_create(&thread3,NULL,write3,NULL);
	
	int count=10;
	while(count-->0)
	{
		printf("main thread\n");
		sleep(1);
	}
	void *thread_wait=0;
	ret=pthread_join(thread1,&thread_wait);
	if(ret==0)
	{
	 	printf("write thread1 wait%u success: %d\n",(unsigned long)thread1,(int)thread_wait);

	}
	else
	{
		printf("write error%s\n",strerror(ret));
	}
	ret=pthread_join(thread2,&thread_wait);
	if(ret==0)
	{
	 	printf("write thread2 wait%u success: %d\n",(unsigned long)thread2,(int)thread_wait);

	}
	else
	{
		printf("write erroe%s\n",strerror(ret));
	}
	pthread_cancel(thread3);
	ret=pthread_join(thread3,&thread_wait);
	if(ret==0)
	{
	 	printf("write thread3 wait%u success: %d\n",(unsigned long)thread3,(int)thread_wait);

	}
	else
	{
		printf("write erroe%s\n",strerror(ret));
	}
	return 0;
}
